package com.kingsoft.dc.khaos.module.spark.util

import java.util.ArrayList

import com.kingsoft.dc.khaos.extender.meta.model.col.DmTableColumn
import org.apache.commons.lang3.StringUtils

import scala.collection.JavaConversions._

object RmdbUtil {


  /**
    * 功能:生成两张表的JOIN 连接sql片段,比如:s.id = t.id AND s.name = t.name
    *
    * @param leftTable  左表名或别名
    * @param rightTable 右表或别名
    * @param joinKeys   连接字段列表
    * @return sql片段
    */
  def joinKeys(leftTable: String, rightTable: String, joinKeys: List[String]): String = {
    val joinSql = new ArrayList[String]
    for (key <- joinKeys) {
      joinSql.add(String.format("%s.\"%s\" = %s.\"%s\"", leftTable, key, rightTable, key))
    }
    StringUtils.join(joinSql, " and  ")
  }

  /**
    * 拼接sql查询字段
    *
    * @param cols       表字段
    * @param alias      表别名
    * @param exceptCols 排除字段(业务字段)
    * @return
    */
  def getSqlCols(cols: java.util.List[DmTableColumn], alias: String, exceptCols: ArrayList[String] = null): String = {
    val queryCols = new ArrayList[String]
    for (field <- cols) {
      if (exceptCols == null || !exceptCols.contains(field.getColName))
        queryCols.add(String.format("%s.\"%s\"", alias, field.getColName))
    }
    StringUtils.join(queryCols, ",")
  }

  def getCols(cols: java.util.List[DmTableColumn], exceptCols: ArrayList[String] = null): String = {
    val queryCols = new ArrayList[String]
    for (field <- cols) {
      if (exceptCols == null || !exceptCols.contains(field.getColName))
        queryCols.add(String.format("\"%s\"", field.getColName))
    }
    StringUtils.join(queryCols, ",")
  }

  def getMysqlCols(cols: java.util.List[DmTableColumn], exceptCols: ArrayList[String] = null): String = {
    val queryCols = new ArrayList[String]
    for (field <- cols) {
      if (exceptCols == null || !exceptCols.contains(field.getColName))
        queryCols.add(String.format("`%s`", field.getColName))
    }
    StringUtils.join(queryCols, ",").replaceAll("\"","")
  }


  def getUpsertCols(leftAlias: String, rightAlias: String, cols: java.util.List[DmTableColumn], exceptCols: List[String] = null): String = {
    val compareCols = new ArrayList[String]()
    for (field <- cols) {
      if (!exceptCols.contains(field.getColName))
        compareCols.add(s"""$leftAlias.\"${field.getColName}\" = $rightAlias.\"${field.getColName}\"""")
    }
    StringUtils.join(compareCols, ",")
  }


  /**
    * 全字段比对，判断数据是否一致
    *
    * @param leftAlias  左表
    * @param rightAlias 右表
    * @param cols       比对字段
    * @return (r.a != l.a or r.b != l.b or r.c != l.c)
    */
  def compareKeys(leftAlias: String, rightAlias: String, cols: Seq[String]): String = {
    if (cols.size == 0)
      return "1 = 1"
    val compareSql = new ArrayList[String]
    for (col <- cols) {
      compareSql.add(s"$leftAlias.${col} != $rightAlias.${col}")
    }
    //    s"( ${StringUtils.join(compareSql, " or ")} )"
    StringUtils.join(compareSql, " or ")
  }

  /**
    * 由于 create table as 建出的表，字段是双引号括起来的 区分大小写，所以需做一次转换
    * 企业云2.2支持oracle大小写敏感，因此注释此方法
    *
    * @param cols
    * @return
    */
  //  def getTmpTabCol(cols:java.util.List[DmTableColumn]): String ={
  //    val compareCols = new ArrayList[String]()
  //    var colName:String = null;
  //    for(field <- cols){
  //      colName = field.getColName
  //      compareCols.add("\""+colName+"\" as "+colName )
  //    }
  //    StringUtils.join(compareCols, ",")
  //  }

}